package mailtest;


import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HttpContext;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

/**
 * @Description:
 * @Author: yangzhiwei
 * @Date: 2021/6/23 18:35
 */
public class FileTest {

    public static void main(String[] args) throws IOException {
        Float f = new Float(0.0);
        System.out.println(String.valueOf(f));
        String url = "https://startpage.soufunimg.com/searchAD/uploadFiles4449818024789105278.mp4";

        String fileName = UUID.randomUUID().toString().replace("-","");
        fileName = fileName + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddhhmm"));
        //System.out.println(fileName);
        URLConnection connection;
        HttpClient httpClient;
        HttpEntity entity;


        ThreadLocal threadLocal;
//        threadLocal.set();
//        threadLocal.get()

        URL remoteUrl = new URL(url);


            //connection = remoteUrl.openConnection();

            //RestTemplate restTemplate = new RestTemplate();
        httpClient = HttpClients.createDefault();
        HttpResponse response = httpClient.execute(new HttpGet(url) );
        entity = response.getEntity();
        BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(entity);
        System.out.println(entity.getContentType().getName());

        //InternalRequest ;
      //  HttpRequestBase httpRequestBase = new HttpRequestBase();

        HttpResponse response1 = httpClient.execute(new HttpOptions(url));
     //   System.out.println(response1.getEntity().getContentType().);

        HttpContext httpContext = new HttpClientContext();
//            httpClient.getURLFile();
//            httpClient.getInputStream();
        //Object object = connection.getContent();


    //HttpEntity entity2 = response.getEntity();


        System.out.println(DigestUtils.md5Hex(bufferedHttpEntity.getContent()));
        System.out.println(DigestUtils.md5Hex(bufferedHttpEntity.getContent()));
        System.out.println(entity.getContentType().getValue());

//        try (InputStream inputStream = entity.getContent()) {
//            int available =  inputStream.available();
//            System.out.println(inputStream.available());
////            System.out.println(connection.getInputStream().markSupported());
////            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
////            FileReader fr;
////            BufferedInputStream bis;
////            InputStreamReader isr;
////            ByteBuffer byteBuffer = ByteBuffer.allocate(100);
////            byteBuffer.array();
////            charBuffer
//
//
////            byte[] inputStreamData = new byte[available];
////            long startInputStramTime = System.nanoTime();
////            inputStream.read(inputStreamData,0,available);
////            long endInputStreamTime = System.nanoTime();
////            System.out.println("signature" + DigestUtils.md5Hex(inputStreamData));
//
//            //inputStream.read()
////            int upperLimit = 1 << 27;  //128M 134217728
////            System.out.println(upperLimit);
////            if (available < 0 || available > upperLimit) {
////                throw new RuntimeException("文件大小超出限制 128M");
////            }\
//            System.out.println("第一次 MD5");
//            System.out.println(DigestUtils.md5Hex(inputStream));
//            byte[] bufferedData = new byte[available];
////
////            System.out.println("bufferedData: " + bufferedData.length);
////            System.out.println(available);
//
//            long startBufferedStremTime = System.nanoTime();
//            //StreamUtils.c
//            BufferedInputStream bis = new BufferedInputStream(inputStream,available);
//
//            // System.out.println(bis.markSupported());
//             bis.mark(available);
//            bis.read(bufferedData);
//            long endBufferedStreamTime = System.nanoTime();
//
//
//            bis.reset();
////            System.out.println("inputStremTime" + (endInputStreamTime - startInputStramTime));
//            System.out.println("bufferedStreamTime" + (endBufferedStreamTime - startBufferedStremTime));
//
//
//
//
//            System.out.println();
////            ByteArrayOutputStream baos = new ByteArrayOutputStream(bufferedData.length);
////            baos.write(bufferedData);
////            baos.writeTo(new FileOutputStream("F:\\c.jpg"));
//            //baos.flush();
//            byte[] test = new byte[1024];
//            for(int read = bis.read(test, 0, 1024); read > -1; read = bis.read(test, 0, 1024)) {
//
//            }
//
//            bis.mark(available);
//            bis.reset();
//            int a = 1;
//            System.out.println(a
//            );
//
//            String signature = DigestUtils.md5Hex(bis);
//
//            System.out.println(bis.available());
//            // System.out.println(bis.);
//
//            System.out.println(signature);
//
//            System.out.println();
//            //bis.read()
//            // bis.close();
//            bis.reset();
//            System.out.println(DigestUtils.md5Hex(bis));
////            available = available + 1;
////            CharBuffer charBuffer = CharBuffer.allocate((available + 10) /2);
////            BufferedReader reader = new BufferedReader (new InputStreamReader(inputStream));
////            reader.read(charBuffer);
////            System.out.println(charBuffer.length());
////            System.out.println();
////            FileOutputStream fis = new FileOutputStream("");
////            FileWriter fw ;
////            OutputStreamWriter osw ;
//            //inputStream.mark();
//            // inputStream.reset();
//
////            br.
//
//            //104857600
//
//
////            System.out.println(available);
////            ByteArrayInputStream bais = new ByteArrayInputStream(new byte[190]);
////
////            TencentUpload.fileUpload("",bais,"");
////
////
////           // OutputStream os = new FileOutputStream()
////            System.out.println("------------------- First signature----------------------");
////            long startTime = System.currentTimeMillis();
////            String signature5 = DigestUtils.md5Hex(inputStream);
////           // System.out.println(signature);
////            System.out.println(inputStream.read());
////            long endTime1 = System.currentTimeMillis();
////            long startTime2 = System.currentTimeMillis();
//
//            /**java.io.IOException: mark/reset not supported
//             * at sun.net.www.http.KeepAliveStream.reset(KeepAliveStream.java:122)
//             * 基于网络的流不支持 mark/reset
//             * */
////            inputStream.mark(0);
////            inputStream.reset();
//
//            /** 如果不想基于网络多读几次就只有基于内存了 **/
//
////
////            String signature2 = DigestUtils.md5Hex(inputStream);
////            long endTime2 = System.currentTimeMillis();
////            System.out.println(endTime1 - startTime);
////            System.out.println(endTime2 - startTime2);
////            System.out.println(signature);
////            System.out.println(signature2);
//            //TencentUpload.fileUpload(fileName,inputStream,null);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new RuntimeException("文件处理异常");
//        }
    }
}
